package org.xiaom.butler.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.service.BaseService;
import org.xiaom.butler.util.Standard;
import org.xiaom.butler.util.Util;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class HotelServlet
 */
public class ConfigServlet extends HttpServlet {
	private static final long serialVersionUID = 1L; 
	private BaseService hotelService;
	private BaseService departmentService;
	private BaseService contentService;
	private String field="config";
	private BaseService configService;
	public ConfigServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		this.hotelService = new BaseService(Hotel.class);
		this.departmentService=new BaseService(Department.class);
		this.contentService=new  BaseService(Content.class);
		super.init();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getRequestURI();
		String function = url.split("\\.")[1];
		String name = url.split("/"+field+"/")[1];
		if (function.equals("do")) { // 请求是个功能性请求
			if ("add.do".equals(name)) {
				add(request, response);
				Department d=(Department)departmentService.get(Util.getQueryParam(request, "department", ""));
				goToList(request, response,d, Standard.REDIRECT);
			} else if ("delete.do".equals(name)) {
				Content c=(Content) contentService.get(Util.getQueryParam(request, "id", ""));
				delete(request, response);
				goToList(request, response,c.getDepartment(), Standard.REDIRECT);
			} else if ("edit.do".equals(name)) {
				Content content=(Content) contentService.get(Util.getQueryParam(request, "id", ""));
				edit(request, response);
				goToList(request, response,content.getDepartment(), Standard.REDIRECT);
			} else {
				request.setAttribute("error_message", "没找到该页面");
				response.sendRedirect("../error.jsp");
			}
		} else if (function.equals("jsp")) {// 要求显示页面
			if (name.equals("add.jsp")) {
				goToAdd(request, response, null); 
			} else if (name.equals("list.jsp")) {
				Department d=(Department)departmentService.get(Util.getQueryParam(request, "department", ""));
				goToList(request, response,d, Standard.FORWARD);
			} else if (name.equals("edit.jsp")) {
				goToEdit(request, response, Standard.FORWARD);
			} else {
				Util.redirectToErrorPage(request, response, "请求的路径不正确");
			}
		}
	}

	private void goToEdit(HttpServletRequest request,
			HttpServletResponse response, boolean forward) {
		Content bean =(Content) contentService.get(Integer.parseInt(Util.getQueryParam(request, "id", "")));
		request.setAttribute("bean", bean); 
		Util.forward(getServletConfig().getServletContext(), request, response,
				"content/edit.jsp", false);
		
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		try {
			Content c =(Content) contentService.get(Util.getQueryParam(request, "id", ""));
			String chName = Util.getQueryParam(request, "ch_name", "");
			String engName = Util.getQueryParam(request, "eng_name", "");
			String chContent=Util.getQueryParam(request, "ch_content", "");
			String engContent=Util.getQueryParam(request, "eng_content", "");
			Integer displayIndex = Integer.parseInt(Util.getQueryParam(request, "display_index", ""));
			String imagePath = Util.getQueryParam(request, "image_path", "");
			
			c.setChName(chName);
			c.setEngName(engName);
			c.setChContent(chContent);
			c.setEngContent(engContent);
			c.setDisplayIndex(displayIndex);
			c.setImagePath(imagePath);  
			
		} catch (Exception e) {
			e.printStackTrace();
			Util.redirectToErrorPage(request, response, "保存错误。请检查输入");
		}
	} 
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			Department d=(Department) departmentService.get(Util.getQueryParam(request, "department", ""));
			Content c=new Content();
			
			String chName = Util.getQueryParam(request, "ch_name", "");
			String engName = Util.getQueryParam(request, "eng_name", "");
			String chContent=Util.getQueryParam(request, "ch_content", "");
			String engContent=Util.getQueryParam(request, "eng_content", "");
			Integer displayIndex = Integer.parseInt(Util.getQueryParam(request, "display_index", ""));
			String imagePath = Util.getQueryParam(request, "image_path", "");
			
			c.setChName(chName);
			c.setEngName(engName);
			c.setChContent(chContent);
			c.setEngContent(engContent);
			c.setDisplayIndex(displayIndex);
			c.setImagePath(imagePath);
			c.setDepartment(d);
			
			departmentService.saveOrUpdate(c);
		} catch (Exception e) {
			e.printStackTrace();
			Util.redirectToErrorPage(request, response, "保存错误。请检查输入");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		JsonObject jo=new JsonObject();
		try {
			String id = (String) request.getParameter("id");
			contentService.delete(Integer.parseInt(id));
			jo.addProperty("state", "success");
			response.getWriter().write(jo.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("{state:faild}");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	private void getOne(HttpServletRequest request, HttpServletResponse response) {

	}

	private void goToList(HttpServletRequest request,
			HttpServletResponse response,Department department, boolean mode)
			throws ServletException, IOException { 
		
		request.setAttribute("department", department);
		String relativePath = "/list.jsp?department="+department.getId();
		if (Standard.FORWARD == mode) {
			Util.forward(getServletConfig().getServletContext(), request,
					response, field+relativePath, false);
		} else {
			Util.redirect(request, response, "list.jsp?department="+department.getId(), false);
		}
	}

	private void goToAdd(HttpServletRequest request,
			HttpServletResponse response, Hotel bean) throws ServletException,
			IOException {
			Department department=(Department) departmentService.get(Integer.parseInt(Util.getQueryParam(request, "department", "")));
			request.setAttribute("department", department);
		Util.forward(getServletConfig().getServletContext(), request, response,
				field+"/add.jsp", false);
	}
}
